import './index.scss'
import { getEffectDepartmentListTree, getEmployeePageList } from '@/api/org/organize'

export default {
  name: 'choose-employee',
  props: {
    // 弹窗显示隐藏
    innerVisible: {
      type: Boolean,
      default: false
    },
    checkIds: {
      type: Array,
      default: []
    },
    multiSelect: { // 是否多选
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      queryParams: {
        departmentId: 0,
        pageNum: 1,
        pageSize: 20
      },
      defaultProps: {
        children: 'children',
        label: 'name',
        value: 'id'
      },
      currentNodekey: '',
      defaultList: [{
        id: 1
      }],
      departmentTreeList: [],
      loading: false,
      total: 0,
      list: [],
      checkList: [],
      defaultCheckIds: [],
      radioObj:{}
    }
  },
  created() {
    this.getDepartmentListTree()
  },
  methods: {
    getDepartmentListTree() {
      getEffectDepartmentListTree({}).then((response) => {
        this.currentNodekey = response[0].id
        this.departmentTreeList = [...response]
        
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.currentNodekey)
          this.handleNodeClick({ id: this.currentNodekey })
        })
      })
    },
    handleNodeClick(data) {
      alert(JSON.stringify(data))
      this.currentNodekey = data.id
      this.queryParams.departmentId = data.id
      this.getList()
    },
    getList1() {
      this.getList()
    },
    // 重置参数
    refreshParams(queryP) {
      this.queryParams = queryP
      this.getList()
    },
    getList() {
      getEmployeePageList(this.queryParams).then(res => {
        this.total = res.total
        this.list = res.records
      })
    },
    // 全部选中
    selectAll(e) {
      if (e.checked) {
        this.checkList.push(...e.records)
        this.defaultCheckIds.push(...e.records.map((item) => item.id))
        this.checkList = Array.from(new Set(this.checkList))
        this.defaultCheckIds = Array.from(new Set(this.defaultCheckIds))
      } else {
        const arr = this.list.map((item) => item.id)
        const arr1 = []
        this.checkList.forEach((item, index) => {
          arr.forEach((it) => {
            if (item.id === it) {
              arr1.push(index)
            }
          })
        })
        arr1.reverse().forEach((item) => {
          this.checkList.splice(item, 1)
          this.defaultCheckIds.splice(item, 1)
        })
      }
    },
    selectChangeEvent(row) {
      let i = -1
      this.checkList.forEach((item, index) => {
        if (item.id === row.row.id) {
          i = index
        }
      })
      if (i === -1) {
        this.checkList.push(row.row)
        this.defaultCheckIds.push(row.row.id)
      } else {
        this.checkList.splice(i, 1)
        this.defaultCheckIds.splice(i, 1)
      }
    },
    radioChange(row){
      this.radioObj = row.row
    },
    radioClick(){
      this.$emit('getEmployeeInfoList',this.radioObj)
    },
    chooseMulti() {
      let employeeData = {
        rowIds: this.defaultCheckIds,
        empArr: this.checkList,
        innerVisible: false
      }
      this.$emit('getEmployeeInfoList',employeeData)
    }
  }
}
